package com.samuel.algorithm.sort;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 选择排序
 */
public class SelectSort {

    public static void main(String[] args) {
        int[] numsPlus = {6, 3, 5, 2, 4, 9, 18, 7};
        selectMin(numsPlus);
    }

    private static void selectMax(int[] nums) {
        if (null == nums || nums.length <= 1) {
            return;
        }
        for (int i = nums.length - 1; i > 0; i--) {
            int selectMaxIndex = i;
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[selectMaxIndex]) {
                    selectMaxIndex = j;
                }
            }
            if (selectMaxIndex != i) {
                int maxNum = nums[selectMaxIndex];
                nums[selectMaxIndex] = nums[i];
                nums[i] = maxNum;
            }
            printArrays(nums);
        }
    }

    private static void selectMin(int[] nums) {
        if (null == nums || nums.length <= 1) {
            return;
        }
        for (int i = 0; i < nums.length - 1; i++) {
            int selectMinIndex = i;
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[j] < nums[selectMinIndex]) {
                    selectMinIndex = j;
                }
            }
            if (selectMinIndex != i) {
                int minNum = nums[selectMinIndex];
                nums[selectMinIndex] = nums[i];
                nums[i] = minNum;
            }
            printArrays(nums);
        }
    }

    private static void printArrays(int[] nums) {
        String collect = Arrays.stream(nums)
                .mapToObj(Integer::toString)
                .collect(Collectors.joining("\t"));
        System.out.println(collect);
    }
}
